<?php
namespace app\index\controller;
use app\model\Admin as AdminModel;
use app\model\AdminToken as AdminTokenModel;
use app\model\Role as RoleModel;
use app\model\Menu as MenuModel;



use think\facade\Cache;
use think\captcha\Captcha;
use think\Db;


class Role extends Base
{   
    // 列表
    public function index(){
        $map_params = [
           
        ];
        $map = getSearchParam($map_params);
        if (isSearchParam('keyword_search')) {
            $map[] = ['name','like','%'.input('keyword_search').'%'];
        }
        $model = new RoleModel();
        $page_param = ['page'=>input('page_index',1),'list_rows'=>input('page_size',10)];
        $list = $model->where($map)->order('create_time','desc')->paginate($page_param)->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['menu_uuids'] = explode(',', $vo['menu_uuids']);
        }

        return apiResult('2000','获取成功',$list);
    }

    // 详情
    public function read($uuid){
        $model = new RoleModel();
        $data = $model->where('uuid',$uuid)->find();
        $data['menu_uuids'] = explode(',', $data['menu_uuids']);
        return apiResult('2000','获取成功',$data);
    }

    // 新增
    public function save(){
        $param = file_get_contents('php://input');
        $param = json_decode($param,true);

        $fields = [
            'must'=>['name','menu_uuids']
        ];
        $save_data = paramFilter($param,$fields);
        if (!empty($save_data['error_msg'])) {
            return apiResult('5000',$save_data['error_msg']);
        }

        $model = new RoleModel();
        // 启动事务 
        Db::startTrans();
        try{
            $save_data['uuid'] = uuidCreate();
            $save_data['create_time'] = date('Y-m-d H:i:s');
            if ( !$model->save($save_data) ) {
                throw new \Exception("保存失败");
            }
           
            // 更新成功 提交事务
            Db::commit();

            return apiResult('2000','提交成功',$save_data['uuid']);
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return apiResult('5000',$e->getMessage());
        }
    }

    // 更新
    public function update(){
        $param = file_get_contents('php://input');
        $param = json_decode($param,true);

        if ( empty($param['uuid']) ) {
            exception('uuid不能为空',400);
        }

        $fields = [
            'nomust'=>['name','menu_uuids']
        ];
        $save_data = paramFilter($param,$fields);
        if (!empty($save_data['error_msg'])) {
            return apiResult('5000',$save_data['error_msg']);
        }
        if (empty($save_data)) {
            return apiResult('5000','无任何修改');
        }

        $model = new RoleModel();
        // 启动事务 
        Db::startTrans();
        try{
            if ( $model->where('uuid',$param['uuid'])->update($save_data) === false ) {
                throw new \Exception("保存失败");
            }
            // 更新成功 提交事务
            Db::commit();

            return apiResult('2000','更新成功');
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return apiResult('5000',$e->getMessage());
        }
    }

    // 删除
    public function delete($uuid){
        $model = new RoleModel();
        Db::startTrans();
        try{
            if ( !$model->where('uuid',$uuid)->delete() ) {
                throw new \Exception("删除失败");
            }
            // 更新成功 提交事务
            Db::commit();
            return apiResult('2000','删除成功');
        } catch (\Exception $e) {
            // 更新失败 回滚事务
            Db::rollback();
            return apiResult('5000',$e->getMessage());
        }
    }
   


}
